package com.metagurukul.metagranth.service;

import java.sql.SQLException;
import java.util.ArrayList;

import junit.framework.TestCase;

import com.metagurukul.metagranth.vo.Answer;
import com.metagurukul.metagranth.vo.Question;
import com.metagurukul.metagranth.vo.Tag;

public class AnswerJUnitTEST extends TestCase{
	/**
	 * this function test the AnswerService class about null check of
	 * saveAnswer function(answer given to function is null )
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here which is not null and then add a test null answer .
	 * 
	 * expected result:function should return error code of null answer.
	 * actual result :function returns 0
	 * 
	 * @author rohit
	 * 
	 */
	public void testSaveAnswerNullCheck() {
		int testUserId = -1;
		try {// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a qustion
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// create a new tag
			Tag newTag = new Tag();
			newTag.setTagName("testtag");
			// save question by service class method which is already tested.
			QuestionService.askQuestion(newQuestion, newTag);
			// make a answer object
			Answer answer = null;
			// call service class method to test.
			int result = AnswerService.saveAnswer(answer);
			// should return 0(error code).
			assertEquals(result, 0);
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {

			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_ANSWER", testUserId);
				CommonJUnit.deleteTestUser();
				CommonJUnit.deleteTestDataFromTags("testtag");
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}
	}

	/**
	 * this function test the AnswerService class about valid answer row inserted
	 * of SaveAnswer function.
	 * secnario:database is having some questions already and we add 1 test
	 * question here which is not null and then add a test answer which is not null.
	 * 
	 * expected result:function should return row updated.
	 * actual result :function returns 1 as only one row is updated.
	 * 
	 * @author rohit
	 * 
	 */
	public void testSaveAnswerValidAnswerRowInserted() {
		int testUserId = -1;
		String tagName = "test";
		try {// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a test question
			Question question = new Question();
			question.setQuestion("testtesttest");
			question.setTitle("test");
			question.setUserId(testUserId);
			// insert a question.
			int qid = CommonJUnit.insertTestData(question);
			// make a new answer.
			Answer answer = new Answer();
			answer.setAnswer("testAnswer");
			answer.setQid(qid);
			answer.setUid(testUserId);
			// call service class method.
			int result = AnswerService.saveAnswer(answer);
			// 1 row updated.
			assertEquals(result, 1);

		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_ANSWER", testUserId);
				CommonJUnit.deleteTestDataFromTags(tagName);
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}
		}
}
	/**
	 * this function test the AnswerService class about changes made in all
	 * relevant tables of save question function (answer posted is valid. )
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here which is not null and 1 valid answer.
	 *  
	 * expected result:function should do all the changes in all relevant
	 * tables. 
	 * actual result :function returns 0
	 * 
	 * @author rohit
	 * 
	 */
	public void testSaveQuestionValidQuestionTableUpdated() {
		int testUserId = -1;
		String tagName = "testTag";
		try {// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a test question
			Question question = new Question();
			question.setQuestion("testtesttest");
			question.setTitle("testTitle");
			question.setUserId(testUserId);
			ArrayList<Answer> answerList = CommonJUnit
			.retrieveTestAnswers(testUserId);
			// all list should be blank
			assertEquals(answerList.size(), 0);
			// insert a question
			int qid = CommonJUnit.insertTestData(question);
			// make a answer.
			Answer answer = new Answer();
			answer.setAnswer("testAnswer");
			answer.setQid(qid);
			answer.setUid(testUserId);
			// service class method
			AnswerService.saveAnswer(answer);
			// retrieve answer list by junit method.
			answerList = CommonJUnit.retrieveTestAnswers(testUserId);
			// all list should have size 1 now.
			assertEquals(answerList.size(), 1);

			// mapping at question table
			// find first answer.
			Answer savedAnswer = answerList.get(0);
			// check is if it is same answer.
			assertTrue(savedAnswer.getAnswer().equals(answer.getAnswer()));
			assertEquals(savedAnswer.getQid(), answer.getQid());
			assertEquals(savedAnswer.getUid(), answer.getUid());

		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {

			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_ANSWER", testUserId);
				CommonJUnit.deleteTestDataFromTags(tagName);
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}

	}

}
